using Lzfy_Refund_Service.Models;
using Lzfy_Refund_Service.Repositories;

namespace Lzfy_Refund_Service.Services
{
    public class PatientInfoService : IPatientInfoService
    {
        private readonly IPatientInfoRepository _patientInfoRepository;
        private readonly ILogger<PatientInfoService> _logger;

        public PatientInfoService(IPatientInfoRepository patientInfoRepository, ILogger<PatientInfoService> logger)
        {
            _patientInfoRepository = patientInfoRepository;
            _logger = logger;
        }

        public async Task<PatientInfoResponse?> GetPatientInfoByIdAsync(string patientId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(patientId))
                {
                    _logger.LogWarning("患者ID不能为空");
                    return null;
                }

                var patientInfo = await _patientInfoRepository.GetPatientInfoByIdAsync(patientId);
                
                if (patientInfo == null)
                {
                    _logger.LogInformation($"未找到患者信息: {patientId}");
                    return null;
                }

                // 转换为响应模型
                var response = new PatientInfoResponse
                {
                    Name = patientInfo.Name,
                    Sex = patientInfo.Sex,
                    DateOfBirth = patientInfo.DateOfBirth,
                    IdNumber = patientInfo.IdNumber,
                    PhoneNumber = patientInfo.PhoneNumber,
                    PatientId = patientInfo.PatientId,
                    Balance = patientInfo.Balance
                };

                _logger.LogInformation($"成功获取患者信息: {patientId}");
                return response;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取患者信息失败: {patientId}");
                throw;
            }
        }
    }
}